using System;
using System.Collections.Generic;
using System.Linq;
namespace WebClient.Models.Repositories
{
	#region using
	using WebClient.Helpers;
	using WebClient.Models.ViewModels;
	using WebClient.Models;
    using WebClient.CF_DAL_WS;
	#endregion
	public class InformationWebRepository
	{
        public List<InformationWebDTO> SelectAll()
		{
			try
			{
                return DALService.Instance.InformationWeb_SelectAll().ToList();
			}
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					GlobalVariables.Logger.Error("InformationWeb - SelectAll: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					GlobalVariables.Logger.Error("InformationWeb - SelectAll: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
			}
            return new List<InformationWebDTO>();
		}

		public InformationWebDTO SelectByID(int id)
		{
			try
			{
                return DALService.Instance.InformationWeb_SelectByID(id.ToString());
			}
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					GlobalVariables.Logger.Error("InformationWeb - SelectByID: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					GlobalVariables.Logger.Error("InformationWeb - SelectByID: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
			}
            return null;
		}

        public List<InformationWebDTO> SelectPaging(int page, int pageSize)
		{
            try
            {
                var lstItem = DALService.Instance.InformationWeb_SelectAll().ToList();
                if (pageSize > 0)
                {
                    int startIndex, itemCount;
                    startIndex = (page - 1) * pageSize;
                    itemCount = pageSize;
                    if (startIndex + itemCount > lstItem.Count)
                        itemCount = lstItem.Count - startIndex;
                    lstItem = lstItem.GetRange(startIndex, itemCount);
                }
                return lstItem;
            }
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					GlobalVariables.Logger.Error("InformationWeb - SelectPaging: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					GlobalVariables.Logger.Error("InformationWeb - SelectPaging: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
			}
            return new List<InformationWebDTO>();
		}

		public bool Insert(InformationWebDTO obj)
		{
			try
			{
                return DALService.Instance.InformationWeb_Insert(obj);
			}
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					GlobalVariables.Logger.Error("InformationWeb - Insert: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					GlobalVariables.Logger.Error("InformationWeb - Insert: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
			}
            return false;
		}

		public bool Delete(int id)
		{
			try
			{
                return DALService.Instance.InformationWeb_Delete(id.ToString());
			}
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					GlobalVariables.Logger.Error("InformationWeb - Delete: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					GlobalVariables.Logger.Error("InformationWeb - Delete: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
			}
            return false;
		}

        public bool Update(InformationWebDTO obj)
        {
            try
            {
                return DALService.Instance.InformationWeb_Update(obj);
            }
            catch
            {
                return false;
            }
        }

		public int GetTotalItem()
		{
			try
			{
                return DALService.Instance.InformationWeb_SelectAll().Length;
			}
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					GlobalVariables.Logger.Error("InformationWeb - GetTotalItem: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					GlobalVariables.Logger.Error("InformationWeb - GetTotalItem: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
			}
            return -1;
		}


		public int GetTotalPage()
		{
			int RecordCount = GetTotalItem();
			int PageSize = WebConfiguration.NumInformationWebPerPage;
			return (RecordCount / PageSize) + ((RecordCount % PageSize == 0) ? 0 : 1);
		}


        public List<InformationWebDTO> RetrieveByID(int id)
		{
			try
			{
                return DALService.Instance.InformationWeb_RetrieveByID(id.ToString()).ToList();
			}
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					GlobalVariables.Logger.Error("InformationWeb - RetrieveByID: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					GlobalVariables.Logger.Error("InformationWeb - RetrieveByID: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
			}
            return new List<InformationWebDTO>();
		}

	}
}